package com.qixin.modules.eaBook.service.impl;

import com.qixin.modules.eaBook.entity.EaAirticketbooks;
import com.qixin.modules.eaBook.entity.EaAirticketbooksDetail;
import com.qixin.modules.eaBook.mapper.EaAirticketbooksDetailMapper;
import com.qixin.modules.eaBook.mapper.EaAirticketbooksMapper;
import com.qixin.modules.eaBook.service.IEaAirticketbooksService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 机票账本
 * @Author: qixin-boot
 * @Date:   2021-07-03
 * @Version: V1.0
 */
@Service
public class EaAirticketbooksServiceImpl extends ServiceImpl<EaAirticketbooksMapper, EaAirticketbooks> implements IEaAirticketbooksService {

	@Autowired
	private EaAirticketbooksMapper eaAirticketbooksMapper;
	@Autowired
	private EaAirticketbooksDetailMapper eaAirticketbooksDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(EaAirticketbooks eaAirticketbooks, List<EaAirticketbooksDetail> eaAirticketbooksDetailList) {
		eaAirticketbooksMapper.insert(eaAirticketbooks);
		if(eaAirticketbooksDetailList!=null && eaAirticketbooksDetailList.size()>0) {
			for(EaAirticketbooksDetail entity:eaAirticketbooksDetailList) {
				//外键设置
				entity.setEaAirticketbooksId(eaAirticketbooks.getId());
				eaAirticketbooksDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaAirticketbooks eaAirticketbooks,List<EaAirticketbooksDetail> eaAirticketbooksDetailList) {
		eaAirticketbooksMapper.updateById(eaAirticketbooks);
		
		//1.先删除子表数据
		eaAirticketbooksDetailMapper.deleteByMainId(eaAirticketbooks.getId());
		
		//2.子表数据重新插入
		if(eaAirticketbooksDetailList!=null && eaAirticketbooksDetailList.size()>0) {
			for(EaAirticketbooksDetail entity:eaAirticketbooksDetailList) {
				//外键设置
				entity.setEaAirticketbooksId(eaAirticketbooks.getId());
				eaAirticketbooksDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaAirticketbooksDetailMapper.deleteByMainId(id);
		eaAirticketbooksMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaAirticketbooksDetailMapper.deleteByMainId(id.toString());
			eaAirticketbooksMapper.deleteById(id);
		}
	}
	
}
